package com.xbongbong.parent.condition.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.field.LinkConditionPoJo;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.parent.abstracts.AbstractConditionFieldHandler;
import com.xbongbong.parent.condition.ProConditionHelp;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.pro.condition.pojo.ConditionDTO;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/3 19:41
 * @since v1.0
 */
@Component
public class ConditionFormLinkDataHandlerImpl extends AbstractConditionFieldHandler {



    private static final Logger LOGGER = LoggerFactory.getLogger(ConditionFormLinkDataHandlerImpl.class);


    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.LINK_DATA.getType());
    }

    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo ) throws XbbException {
        businessRuleInfoPojo.setObject(data.get(fieldAttrEntity.getAttr()));
        Object object = getLinkData(businessRuleInfoPojo, fieldAttrEntity, targetField);
        if (Objects.equals(FieldTypeEnum.LINK_DATA.getType(), fieldAttrEntity.getFieldType()) && Objects.equals(fieldAttrEntity.getFieldType(), targetField.getFieldType())) {
            object = data.get(fieldAttrEntity.getAttr());
            if (Objects.equals(0, object)) {
                return null;
            }
        }
        return object;
    }


    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity,
                                         FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterConditionsPoJo) throws XbbException {
        Object object = getLinkData(businessRuleInfoPojo, fieldAttrEntity, targetField);
        return object;
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        if (Objects.equals(ConditionEnum.IN.getSymbol(), filterCondition.getSymbol()) || Objects.equals(ConditionEnum.NOIN.getSymbol(), filterCondition.getSymbol())) {
            if (filterCondition.getValue() instanceof List) {
                List<String> idList = new ArrayList<>();
                // 关联业务单选的格式
                List<Map<String, Object>> linkSingleList = (List) filterCondition.getValue();
                for (int i = 0; i < linkSingleList.size(); i++) {
                    Map<String, Object> linkSingle = linkSingleList.get(i);
                    if (Objects.nonNull(linkSingle) && Objects.nonNull(linkSingle.get("id"))) {
                        idList.add(linkSingle.get("id").toString());
                    }
                }
                return idList;
            }
        }
        return ProConditionHelp.getIdByLinkBusiness(filterCondition.getValue());
    }

    @Override
    public Object handlerPreConditionValue(FieldAttrEntity thisField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
       return this.handlerFixedValue(thisField, businessRuleInfoPojo, filterCondition);
    }

    private Object getLinkData(BusinessRuleInfoPojo businessRuleInfoPojo, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField) throws XbbException {
        Object object = businessRuleInfoPojo.getObject();
        if (ProConditionHelp.validateIsLinkData(fieldAttrEntity.getFieldType(), targetField.getFieldType())
                && Objects.nonNull(object) && !Objects.equals(0, object) && object instanceof Number) {
            LinkInfoPojo linkInfoPojo = fieldAttrEntity.getLinkInfo();
            String key = object.toString();
            if (Objects.equals(SaasMarkEnum.SAAS.getCode(), linkInfoPojo.getLinkSaasMark())) {
                key = linkInfoPojo.getLinkBusinessType() + "_" + key;
            }
            if (Objects.isNull(businessRuleInfoPojo.getFormDataMap())) {
                return null;
            }
            PaasFormDataEntityExt formData = businessRuleInfoPojo.getFormDataMap().get(key);
            if (Objects.isNull(formData)) {
                LOGGER.error("error is empty：" + key);
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_DATA_NOT_EXIST);
            }
            String linkKey = linkInfoPojo.getLinkKey();
            if (Objects.equals(FieldTypeEnum.SERIALNO.getAlias(), linkKey)) {
                return formData.getSerialNo();
            }
            JSONObject targetData = formData.getData();
            if (Objects.nonNull(targetData)) {
                object = targetData.get(linkKey);
                return object;
            }
        } else if (Objects.isNull(object) || Objects.equals(0, object) || StringUtil.isEmpty(object.toString())) {
            return null;
        }
        return Long.valueOf(object.toString());
    }

    @Override
    public Object handlerRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, LinkConditionPoJo linkConditionPoJo, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        return ProConditionHelp.getIdByLinkBusiness(value);
    }

    @Override
    public Object handlerLinkDataFixedValue(String corpid, FieldAttrEntity targetField, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        return value;
    }

    @Override
    public Object handlerBusinessRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, LinkConditionPoJo linkConditionPoJo, Object value, Long linkFormId, Integer linkBusinessType, Integer linkSaasMark) throws XbbException {
        return ProConditionHelp.getIdByLinkBusiness(value);
    }

    @Override
    public Object handlerBusinessDataFixedValue(String corpid, FieldAttrEntity targetField, ConditionDTO conditionDTO, Object value) throws XbbException {
        return value;
    }
}
